package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cuccpay.ebank.CuccPayMessage;
import com.gopay.common.util.MoneyUtil;

/**
 * 联通沃前台银行回应
 * 
 * @CuccPayFrontRes.java
 * @author WangHongFei
 */
@Controller
@Scope("prototype")
public class CuccPayFrontRes extends BankResBase {

	private static Log logger = LogFactory.getLog(CuccPayFrontRes.class);
	
    @RequestMapping(value = "/cuccPayFrontRes")
    protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
    	logger.error("--------------- CuccPayFrontRes bankback  ---------------");
		return super.execute(request, response);
    }
    
    @Override
    protected BaseMessage buildMessage(HttpServletRequest request) {
        CuccPayMessage message = new CuccPayMessage();
	    message.setBank(BankConstants.BANK_LOGO_CUCCPAY);
		StringBuffer sb = new StringBuffer();
		sb.append("acountDate=").append(request.getParameter("acountDate"));
		sb.append("|amount=").append(request.getParameter("amount"));
		sb.append("|bankAcountNo=").append(request.getParameter("bankAcountNo"));
		sb.append("|bankAcountName=").append(request.getParameter("bankAcountName"));
		sb.append("|charSet=").append(request.getParameter("charSet"));
		sb.append("|errDis=").append(request.getParameter("errDis"));
		sb.append("|goodId=").append(request.getParameter("goodId"));
		sb.append("|goodsDesc=").append(request.getParameter("goodsDesc"));
		sb.append("|goodsName=").append(request.getParameter("goodsName"));
		sb.append("|interfaceVersion=").append(request.getParameter("interfaceVersion"));
		sb.append("|merNo=").append(request.getParameter("merNo"));
		sb.append("|merchantTime=").append(request.getParameter("merchantTime"));
		sb.append("|merExtend=").append(request.getParameter("merExtend"));
		sb.append("|merUserId=").append(request.getParameter("merUserId"));
		sb.append("|mobileNo=").append(request.getParameter("mobileNo"));
		sb.append("|orderDate=").append(request.getParameter("orderDate"));
		sb.append("|orderNo=").append(request.getParameter("orderNo"));
		sb.append("|payAcountDetail=").append(request.getParameter("payAcountDetail"));
		sb.append("|payBankCode=").append(request.getParameter("payBankCode"));
		sb.append("|payJnlno=").append(request.getParameter("payJnlno"));
		sb.append("|payProAmt=").append(request.getParameter("payProAmt"));
		sb.append("|payTime=").append(request.getParameter("payTime"));
		sb.append("|remark=").append(request.getParameter("remark"));
		sb.append("|respMode=").append(request.getParameter("respMode"));
		sb.append("|signType=").append(request.getParameter("signType"));
		sb.append("|transRst=").append(request.getParameter("transRst"));
		message.setPlainMessage(sb.toString());
		message.setSignMsg(request.getParameter("signMsg"));
		message.setOrderId(request.getParameter("orderNo"));
		message.setBankOrderId(request.getParameter("orderNo"));
		message.setAmount(new BigDecimal(request.getParameter("amount")));
		message.setFront(true);
        //银行业务产品00个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
        return message;
    }

    @Override
    protected OrderInfo buildOrderInfo(HttpServletRequest request,Result result) {
    	String retCode = request.getParameter("transRst");
    	String amount = request.getParameter("amount");
        amount = MoneyUtil.formatAmt(amount);
        OrderInfo orderInfo = new OrderInfo();
        if(retCode!=null && retCode.equals("1")){
            orderInfo.setOrderId(request.getParameter("orderNo"));
            orderInfo.setAmt(amount);
            orderInfo.setNotifyNum("1");
            orderInfo.setPayStatus("00");
            orderInfo.setRetCode("00");
            orderInfo.setRetMsg(request.getParameter("errDis"));
        }else{
            orderInfo.setOrderId(request.getParameter("orderNo"));
            orderInfo.setAmt(amount);
            orderInfo.setNotifyNum("1");
            orderInfo.setPayStatus("01");
            orderInfo.setRetCode("01");
            orderInfo.setRetMsg("");
        }        
        orderInfo.setCardType("");// 信用卡类型
        return orderInfo;
    }

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("orderNo"))
    			|| StringUtils.isEmpty(request.getParameter("amount"))
                || StringUtils.isEmpty(request.getParameter("transRst"))
                || StringUtils.isEmpty(request.getParameter("signMsg"))
                || StringUtils.isEmpty(request.getParameter("merNo"))) {
		return false;
		}
		return true;
	}
}
